import reqwest from "reqwest";

const apiUrl = `http://api.wymusic.yutao2012.com`;

const ajax = ( method = 'get', url, opts = {} ) => {
    const data = Object.assign({ref:Date.now()},opts);
    return new Promise(( resolve, reject ) => {
        reqwest({
            method: method,
            url: `${apiUrl}${url}`,
            data: data,
            dataType: "json",
            crossOrigin: true,
            withCredentials:true
        }).then( ret => {
            ret.code === 200 ? resolve(ret) : reject(ret.code);
        }).catch( error => {
            reject(500);
        });
    });
};

const get = ({ url , data }) => {
    return ajax.apply(
        null, ["get", url, data ]
    );
};

const post = ({ url , data }) => ajax.apply(
    null, ["post", url, data ]
);

export default {
    get,
    post
}

